package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.HouseType;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.House;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.model.UserCollection;
import com.wuming.house.service.HouseService;
import com.wuming.house.service.SecondhouseService;
import com.wuming.house.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/9/17 18:44
 * @Description:
 */
@Controller
@RequestMapping("/web/userCollection")
public class UserCollectionController {

    @Autowired
    private UserCollectionService userCollectionService;

    @Autowired
    private HouseService houseService;

    @Autowired
    private SecondhouseService secondhouseService;

    @ResponseBody
    @RequestMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Long userid, Integer houseType, @RequestParam(defaultValue = "1")int pageIndex,
                             @RequestParam(defaultValue = "10") int pageSize){
        List<House> houses = new ArrayList<>();
        List<Secondhouse> secondhouses = new ArrayList<>();
        try {
            List<UserCollection> userCollections = userCollectionService.getUserCollectionByPage(userid,houseType,pageIndex,pageSize);
            int total = userCollectionService.getTotal(userid,houseType);
            if (userCollections.size()!=0 && null!=userCollections && houseType== HouseType.NEW_HOUSE.getValue()){
                for (UserCollection userCollection : userCollections){
                    House house = houseService.getHouseById(userCollection.getHouseId());
                    houses.add(house);
                }
                return CommonResult.createSuccessResult(houses,total,"查询新房收藏列表成功");
            }else {
                for (UserCollection userCollection : userCollections){
                    Secondhouse secondhouse = secondhouseService.getSecondhouseById(userCollection.getHouseId());
                    secondhouses.add(secondhouse);
                }
                return CommonResult.createSuccessResult(secondhouses,total,"查询二手房收藏列表成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.createFailureResult("查询浏览记录失败");
        }
    }

    @ResponseBody
    @RequestMapping("/isCollect/{houseId}")
    public JsonResult isCollect(@PathVariable Long houseId,@RequestParam Integer houseType,@RequestParam Long userid){
        List<UserCollection> collections = userCollectionService.getUserCollectionById(userid, houseId, houseType);
        if (null != collections && !collections.isEmpty()) {
            return new JsonResult(null, "已经收藏", true);
        }else {
            return new JsonResult(null,"还未收藏",false);
        }
    }
}
